/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package g5.factory;

import g5.form.Field;
import g5.form.Section;
import g5.fw.HBox;
import g5.fw.IJSon;
import g5.fw.IPage;
import g5.page.Page;
import java.sql.Connection;

/**
 *
 * @author hue
 */
public abstract class PageFactory {
    private static PageFactory factory;
    
    private PageFactory() {
        
    }
    
    protected HBox newFullBox(IJSon both) {
        return new HBox(null, null, both);
    }
    
    protected HBox newLRBox(IJSon left, IJSon right) {
        return new HBox(left, right, null);
    }

    public abstract IPage createPage(Connection con, final long pk);
    
    public static PageFactory newInstance() {
        if(factory == null) {
            factory = new PageFactory() {

                @Override
                public IPage createPage(Connection con, long pk) {
                    IPage page = new Page();

                    Section s1 = new Section();
                    s1.setName("s1");
                    s1.setType("FORM");
                    
                    Field f1 = new Field();
                    f1.setPK("1");
                    f1.setName("f1");
                    f1.setType("STRING_SMALL");
                    f1.setValue("v1");
                    
                    Field f2 = new Field();
                    f2.setPK("2");
                    f2.setName("f2");
                    f2.setType("STRING_LARGE");
                    f2.setValue("v2");

                    s1.getHBoxes().add(newLRBox(f1, null));
                    s1.getHBoxes().add(newLRBox(null, f2));
                    
                    s1.getHBoxes().add(newLRBox(f2, f1));
                    s1.getHBoxes().add(newFullBox(f1));
                    
                    page.getHBoxes().add(newLRBox(s1, s1));
                    page.getHBoxes().add(newFullBox(s1));
                    page.getHBoxes().add(newLRBox(s1, s1));

                    return page;
                }
            };
        }
        return factory;
    }
}
